package com.sec.bnjey.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;
import android.util.Log;

/**
 * Log for rabiaband.
 * 
 * @author jay
 * 
 */
public class RbLog {

	/** Disable Debug */
	// public static boolean D = false;
	static String LogFileName = "log_vtalk.txt";
	/** Enable Debug */
	public static final boolean D = true;

	// enable file log
	// release = false / debug = true
	public static final boolean ENABLE_FILE_LOG = true;

	// public static final boolean D_TEST_VERSION = true;
	// public static final boolean DEBUG_TESTPOPUP_USE_FOR_CALL = false;
	// public static final boolean DEBUG_DONOTSHOWCALLERINFO = false;
	//
	// // send email when error occur
	// public static final boolean DEBUG_ENABLE_SEND_EMAIL_LOG = true;
	// public static final boolean TEST_FAKE_CALL = false;
	// public static final String TEST_FAKE_CALL_NUMBER = "01090708916";

	public static void d(String tag, String msg) {
		if (D && tag != null && msg != null)
			Log.d(tag, msg);

		if (ENABLE_FILE_LOG)
			appendLog(tag + " D " + msg);

	}

	public static void e(String tag, String msg) {
		if (D && tag != null && msg != null)
			Log.e(tag, msg);

		if (ENABLE_FILE_LOG)
			appendLog(tag + " E " + msg);
	}

	// public static void d(Activity activity, String msg) {
	// if (D && activity != null && msg != null)
	// Log.d(activity.getLocalClassName(), msg);
	//
	// }
	public static void appendLog(String text) {

		SimpleDateFormat sdf = new SimpleDateFormat("MMdd_HHmmss");
		String currentDateandTime = sdf.format(new Date());

		String logBuf = currentDateandTime + " " + text;

		// File logFile = new File("sdcard/log.file");
		File logFile = new File(Environment.getExternalStorageDirectory()
				.toString(), LogFileName);
		if (!logFile.exists()) {
			try {
				logFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			// BufferedWriter for performance, true to set append to file flag
			BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
					true));
			buf.append(logBuf);
			buf.newLine();
			buf.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// public static void appendLog(String text) {
	//
	// SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
	// String currentDateandTime = sdf.format(new Date());
	//
	// String logBuf = currentDateandTime + " " + text;
	//
	// // File logFile = new File("sdcard/log.file");
	// File logFile = new File("/mnt/sdcard/vtalk/log.txt");
	// if (!logFile.exists()) {
	// try {
	// logFile.createNewFile();
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }
	// try {
	// // BufferedWriter for performance, true to set append to file flag
	// BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
	// true));
	// buf.append(logBuf);
	// buf.newLine();
	// buf.close();
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }
}
